package com.meixiang.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.activity.homes.shopping.VerifyGoodsOrderActivity;
import com.meixiang.entity.coupon.CouponEntity;
import com.meixiang.tool.Tool;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 优惠券
 */
public class CouponAdapter extends com.meixiang.recyclerview.RecyclerAdapter<CouponAdapter.MyViewHolder> {

    private List<CouponEntity> couponList;
    private Context context;
    private String formCode; // 从哪个页面跳转过来的标示 1.代表从优惠券点击进入；2.代表选择使用优惠券
    private Activity mActivity;
    private String canUse ;

    public CouponAdapter(Context context, Activity mActivity, String formCode,String canUse) {
        this.context = context;
        this.formCode = formCode;
        this.mActivity = mActivity;
        this.canUse = canUse ;
    }

    public void refreshCouponData(List<CouponEntity> couponList){
        this.couponList = couponList ;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_coupon, parent, false));
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if ("1".equals(formCode)) {
            if ("0".equals(couponList.get(position).getCouponIsused())) {
                //classId 1：满减，2：立减
                if ("2".equals(couponList.get(position).getClassId())) { //立减
                    setCouponYellow(holder, position, View.INVISIBLE, R.mipmap.coupon1, R.color.coupon_yellow);
                }
                if ("1".equals(couponList.get(position).getClassId())) { //满减
                    setCouponYellow(holder, position, View.INVISIBLE, R.mipmap.manjian, R.color.coupon_red);
                }
            } else if ("1".equals(couponList.get(position).getCouponIsused())) { //可用
                if ("2".equals(couponList.get(position).getClassId())) { //立减
                    setCouponUsed(holder, position, View.INVISIBLE, R.mipmap.keyong);
                }
                if ("1".equals(couponList.get(position).getClassId())) { //满减
                    setCouponUsed(holder, position, View.INVISIBLE, R.mipmap.keyong);
                }
            } else if ("2".equals(couponList.get(position).getCouponIsused())) {
                if ("2".equals(couponList.get(position).getClassId())) { //立减
                    setCouponUsed(holder, position, View.INVISIBLE, R.mipmap.guoqi);
                }
                if ("1".equals(couponList.get(position).getClassId())) { //满减
                    setCouponUsed(holder, position, View.INVISIBLE, R.mipmap.guoqi);
                }
            }
        } else {
            Log.i("isUsed","isUsed ="+couponList.get(position).getCouponIsused());
            if("".equals(couponList.get(position).getCouponIsused())){
                if ("0".equals(canUse)) {//可用
                    if ("2".equals(couponList.get(position).getClassId())) { //立减
                        setCouponYellow(holder, position, View.VISIBLE, R.mipmap.coupon1, R.color.coupon_yellow);
                    }
                    if ("1".equals(couponList.get(position).getClassId())) { //满减
                        setCouponYellow(holder, position, View.VISIBLE, R.mipmap.manjian, R.color.coupon_red);
                    }
                } else{ //已使用
                    if ("2".equals(couponList.get(position).getClassId())) { //立减
                        setCouponUsed(holder, position, View.INVISIBLE, R.mipmap.keyong);
                    }
                    if ("1".equals(couponList.get(position).getClassId())) { //满减
                        setCouponUsed(holder, position, View.INVISIBLE, R.mipmap.keyong);
                    }
                }
            }else{
                if ("0".equals(couponList.get(position).getCouponIsused())) {//可用
                    if ("2".equals(couponList.get(position).getClassId())) { //立减
                        setCouponYellow(holder, position, View.VISIBLE, R.mipmap.coupon1, R.color.coupon_yellow);
                    }
                    if ("1".equals(couponList.get(position).getClassId())) { //满减
                        setCouponYellow(holder, position, View.VISIBLE, R.mipmap.manjian, R.color.coupon_red);
                    }
                } else{ //已使用
                    if ("2".equals(couponList.get(position).getClassId())) { //立减
                        setCouponUsed(holder, position, View.INVISIBLE, R.mipmap.keyong);
                    }
                    if ("1".equals(couponList.get(position).getClassId())) { //满减
                        setCouponUsed(holder, position, View.INVISIBLE, R.mipmap.keyong);
                    }
                }
            }
        }
        holder.mCbCouponCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent();
                mIntent.putExtra(VerifyGoodsOrderActivity.CP_ID, couponList.get(position).getCouponId());
                mIntent.putExtra(VerifyGoodsOrderActivity.CP_PRICE, couponList.get(position).getCouponPrice());
                // 设置结果，并进行传送
                mActivity.setResult(Activity.RESULT_OK, mIntent);
                mActivity.finish();
            }
        });

    }
    /**
     * 已用 和过期
     *
     * @param holder
     * @param position
     */
    private void setCouponUsed(MyViewHolder holder, int position, int visibility, int mipmapId) {
        holder.mCbCouponCheck.setVisibility(visibility);
        holder.mRlCoupon.setBackgroundResource(mipmapId);
        holder.mTvCouponTitle.setText(couponList.get(position).getCouponTitle());
        holder.mTvCoupon.setText(couponList.get(position).getClassName()+"券");
        holder.mTvCouponTitle.setTextColor(ContextCompat.getColor(context, R.color.gray_d));
        holder.mTvUnit.setTextColor(ContextCompat.getColor(context, R.color.gray_d));
        holder.mTvCouponContent.setText(couponList.get(position).getCouponDesc());
        holder.mTvCouponContent.setTextColor(ContextCompat.getColor(context, R.color.gray_b));
        holder.mTvCouponTime.setTextColor(ContextCompat.getColor(context, R.color.gray_b));
        BigDecimal b = new BigDecimal(couponList.get(position).getCouponPrice());
        int number = b.setScale(2, BigDecimal.ROUND_HALF_UP).intValue();
        holder.mTvCouponMoney.setText(number + "");
        holder.mTvCouponMoney.setTextColor(ContextCompat.getColor(context, R.color.gray_b));
        holder.mTvCouponTime.setText(Tool.getCouponData(couponList.get(position).getStartTime()) + "-" + Tool.getCouponData(couponList.get(position).getEndTime()));
    }

    /**
     * 可用
     *
     * @param holder
     * @param position
     * @param visibilityId
     * @param mipmapId
     * @param colorId
     */
    private void setCouponYellow(MyViewHolder holder, int position, int visibilityId, int mipmapId, int colorId) {
        holder.mCbCouponCheck.setVisibility(visibilityId);
        holder.mRlCoupon.setBackgroundResource(mipmapId);
        holder.mTvCouponTitle.setText(couponList.get(position).getCouponTitle());
//        holder.mTvCouponTitle.setText(couponList.get(position).getStoreName());
        holder.mTvCoupon.setText(couponList.get(position).getClassName()+"券");
        holder.mTvUnit.setTextColor(ContextCompat.getColor(context, colorId));
        holder.mTvCouponContent.setText(couponList.get(position).getCouponDesc());
        BigDecimal b = new BigDecimal(couponList.get(position).getCouponPrice());
        int number = b.setScale(2, BigDecimal.ROUND_HALF_UP).intValue();
        holder.mTvCouponMoney.setText(number + "");
        holder.mTvCouponMoney.setTextColor(ContextCompat.getColor(context, colorId));
        holder.mTvCouponTime.setText(Tool.getCouponData(couponList.get(position).getStartTime()) + "-" + Tool.getCouponData(couponList.get(position).getEndTime()));
    }

    @Override
    public int getItemCount() {
        return couponList == null ? 0 : couponList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_coupon)
        TextView mTvCoupon;
        @Bind(R.id.tv_unit)
        TextView mTvUnit;
        @Bind(R.id.tv_coupon_money)
        TextView mTvCouponMoney;
        @Bind(R.id.rl_coupon)
        RelativeLayout mRlCoupon;
        @Bind(R.id.tv_coupon_title)
        TextView mTvCouponTitle;
        @Bind(R.id.cb_coupon_check)
        CheckBox mCbCouponCheck;
        @Bind(R.id.tv_coupon_content)
        TextView mTvCouponContent;
        @Bind(R.id.tv_coupon_time)
        TextView mTvCouponTime;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
